import random

import pygame
import math
import sys

# 初始化pygame
from game.circle.config import config
from game.snake.entity.block import Block

pygame.init()

# 设置窗口大小
# screen_width, screen_height = 1000, 800
screen_width = config['screen_width']
screen_height = config['screen_height']
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置窗口标题
pygame.display.set_caption(config['title'])
# 设置中心和半径
# center_x, center_y = screen_width/2, screen_height/2
# radius = 150
center_x = config['center_x']
center_y = config['center_y']
radius = config['radius']

# 设置角度和角速度
angle = 0
angle_speed = 0.01

# 船的图像（这里用一个矩形代替）
# ship = pygame.Rect(0, 0, ship_size, ship_size)
# 圆形ship的半径
ship_size = config['ship_size']
ship_radius = config['ship_radius']
screen.fill(config['WHITE'])
# 获取当前时间
start_time = pygame.time.get_ticks()
# 定义颜色灰色
color = (128, 128, 128)
# 定义方块的大小和间隔
block_size = config['block_size']
gap = config['gap']
# 计算每个方块的位置
blocks_colors = []
n = config['n']
number = 1
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 定义文本
text = "123"  # 要显示的数字或文本
font_size = 36
font = pygame.font.Font(None, font_size)

for i in range(n):
    for j in range(n):
        x = j * (block_size + gap) + (screen_width - n * (block_size + gap))
        y = i * (block_size + gap)
        blocknew = Block(x, y, block_size, block_size,(0,100,0))
        # 给方块进行编号
        blocknew.setText(pygame,str(number))
        number += 1
        # 根据方块编号设置颜色
        d = int( blocknew.text)
        #随机正整数0-100
        color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        blocknew.color = color
        blocks_colors.append(blocknew)

# 圆圈集合
blocks_cirs = []
for s_num in range(n):
    block_cir = Block(0,0,ship_size,ship_size,(0,100,0))
    block_cir.setText(pygame,str(s_num))
    # 根据方块编号设置颜色
    d = int( block_cir.text)
    color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
    block_cir.color = color
    block_cir.angle = 0  # 每个船的初始角度
    block_cir.radius = radius + s_num * 35  # 每个船的半径
    block_cir.angle_speed = angle_speed / (block_cir.radius / 100.0)  # 根据半径调整角速度

    blocks_cirs.append(block_cir)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # 判断是否按下了键盘的h键
        if event.type == pygame.KEYDOWN and event.key == pygame.K_h:
            # 设置颜色为天蓝色
            for block in blocks_colors:
                color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
                block.color = color
        # 判断鼠标点击位置是否在方块列表中
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 获取鼠标点击位置
            mouse_pos = pygame.mouse.get_pos()
            print("鼠标点击位置：", mouse_pos)
            # 判断鼠标点击位置是否在方块列表中
            isc = False
            for block in blocks_colors:
                if block.collidepoint(mouse_pos):
                    # 根据方块编号设置颜色
                    color = block.color
                    text = (int(block.text)-1)%4
                    print("text",text)
                    # blocks_cirs中text与text相同则修改其color
                    for cir in blocks_cirs:
                        if int(cir.text) == text:
                            cir.color = color

                    isc = True

            if isc == False:
                color = (0, 0, 0)
    # 清除屏幕
    # 获取当前时间
    current_time = pygame.time.get_ticks()
    if current_time - start_time > 1000*0.5:
        start_time = current_time
        # 设置颜色为天蓝色
        for block in blocks_colors:
            color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
            block.color = color

    screen.fill((255, 255, 255))
    # 更新角度
    angle += angle_speed

    # 添加一个固定圆心的圆
    # pygame.draw.circle(screen, (255,0,0), (center_x, center_y), ship_radius+20)
    # 添加一个 五角星，颜色为红色，不旋转
    pygame.draw.polygon(screen, (255,0,0), [(center_x + 50 * math.cos(angle + 2 * math.pi / 5 * i),
                                             center_y + 50 * math.sin(angle + 2 * math.pi / 5 * i))
                                            for i in range(5)])
    for ship in blocks_cirs:
        num = int(ship.text)
        # 每个船的转动速度不同，半径越大转动越慢
        # 更新每个船的角度
        ship.angle += ship.angle_speed
        ship.centerx = center_x + ship.radius * math.cos(ship.angle)
        ship.centery = center_y + ship.radius * math.sin(ship.angle)
        # print("ship.centerx",ship.centerx, "ship.centery",ship.centery, "ship.text",ship.text)
        pygame.draw.circle(screen, ship.color, (int(ship.centerx), int(ship.centery)), ship_radius)
        # 渲染文本
        text_surface = font.render(ship.text, True, BLACK)
        text_rect = text_surface.get_rect(center=(ship.centerx , ship.centery ))
        # 绘制文本
        screen.blit(text_surface, text_rect)
        print(ship.show())


    # 绘制每个方块
    for block in blocks_colors:
        block.draw(screen,pygame)
    # 时间暂停1秒中
    # pygame.time.delay(1000)
    # 更新屏幕
    pygame.display.flip()
